package edu.uwlax.cs.oayonlinestore.client.gui.stock;

import java.util.Date;

import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;

import edu.uwlax.cs.oayonlinestore.client.OnlinestoreRService;
import edu.uwlax.cs.oayonlinestore.client.User;
import edu.uwlax.cs.oayonlinestore.client.utils.DateEdit;
import edu.uwlax.cs.oayonlinestore.client.utils.DefaultAsyncCallback;
import edu.uwlax.cs.oayonlinestore.client.utils.FormTable;
import edu.uwlax.cs.oayonlinestore.client.utils.TitlePage;
import edu.uwlax.cs.oayonlinestore.client.utils.DateEdit.InvalidDateException;
import edu.uwlax.cs.oayonlinestore.vo.ProductDetailsVO;

public class PageStockEditItem extends TitlePage {

	String idsSaved;

	int itemId;

	int subID;

	ProductDetailsVO prod;

	TextBox name, available, date, brand, prodsize;

	private DateEdit expDate;

	TextArea desc;

	public String getAnchor(int i, int j) {
		return getAnchor(i + "-" + j);
	}

	private class ItemForm extends FormTable {
		public ItemForm() {
			super();
			addInput("Brand", brand = new TextBox());
			addInput("Product Name", name = new TextBox());
			addInput("Product Size", prodsize = new TextBox());
			addInput("Number of Items Available", available = new TextBox());
			addInput("Expected Date of next Delivery of this Item",
					expDate = new DateEdit(true, false));
			if (itemId != 0)
				addText("Price", prod.getPrice() + "");
			else
				addText("Price", "To be set by sales department!");
			boolean isIndustrail = User.get().isIsIndustrialCustomer();
			if (itemId != 0) {
				if (isIndustrail)
					addText("Discount", prod.getIndustrialDiscount() + "");
				else
					addText("Discount", prod.getNormalDiscount() + "");
			} else
				addText("Discount", "To be set by sales department!");

			addInput("Description", desc = new TextArea());
			addButton("Save", null);
		}

		public void onButtonClicked() throws InvalidDateException {

			prod.setName(name.getText());
			prod.setDescription(desc.getText());
			prod.setBrand(brand.getText());
			prod.setSize(prodsize.getText());
			prod.setIndustrialDiscount(0f);
			prod.setNormalDiscount(0f);
			prod.setPrice(0f);
			prod.setInStockCount(Integer.parseInt(available.getText()));
			prod.setExpBackInStock(expDate.getDate());
			OnlinestoreRService.Util.getInstance().addEditProduct(prod, subID,
					getSaveHandler());
		}

		public void onDataLoad(Object data) {
		}
	}

	ItemForm table;

	protected void create() {
		setTitle("Edit Item");
		add(new ItemForm());

	}

	public class CategoryAsycHandler extends DefaultAsyncCallback {

		public void onSuccess(Object o) {
			ProductDetailsVO[] prods = (ProductDetailsVO[]) o;
			int i = 0;
			boolean flag = false;
			while (i < prods.length && !flag) {
				if (prods[i].getId() == itemId)
					flag = true;
				i++;
			}
			prod = prods[i - 1];
			name.setText(prod.getName());
			brand.setText(prod.getBrand());
			desc.setText(prod.getDescription());
			prodsize.setText(prod.getSize());
			available.setText(prod.getInStockCount() + "");
			expDate.setDate(prod.getExpBackInStock());
		}
	}

	public boolean onShow(String ids) {

		idsSaved = ids;
		String[] idsSplit = ids.split("-");
		String id1 = idsSplit[0];
		String id2 = idsSplit[1];
		int subId = Integer.parseInt(id1);
		itemId = Integer.parseInt(id2);
		subID = subId;

		setTitle((itemId == 0) ? "Create Item" : "Edit Item");
		if (itemId != 0) {

			OnlinestoreRService.Util.getInstance().getProductDetailList(subId,
					new CategoryAsycHandler());

		} else {
			name.setText("");
			brand.setText("");
			desc.setText("");
			prodsize.setText("");
			available.setText("");
			expDate.setDate(new Date());
			prod = new ProductDetailsVO(0, "", "", "", "", 0, 0f, 0f, 0f,
					new Date());
		}

		return true;

	}

	public String getID() {
		return "EditItem";
	}

}
